package com.nt.train.config;
import com.nt.train.config.properties.AliyunOssProperties;
import com.nt.train.config.properties.MinioOssProperties;
import com.nt.train.service.OssService;
import com.nt.train.service.impl.AliyunOssServiceImpl;
import com.nt.train.service.impl.MinioServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 读取配置
 * @author hlf
 * @version 1.0
 * @date 2022/8/4 13:47
 *  @ConditionalOnBean 当指定的 Bean 存在时，则实例化当前 Bean。
    @ConditionalOnMissingBean
    与 @ConditionalOnBean 相反，当指定的 Bean 不存在时，则实例化当前 Bean。
     @ConditionalOnClass
     当指定的类名在类路径上存在时，则实例化当前 Bean。
     @ConditionalOnMissingClass
     当指定的类名在类路径上不存在时，则实例化当前 Bean
     */
@Configuration
public class OssConfiguration {

    @Bean(value = "aliyunOssProperties")
    @ConfigurationProperties(prefix = "oss.aliyun")
    /**
     * @ConditionalOnProperty(value = "oss.aliyun.endPoint") 表示，
     * 在配置文件 .properties 或 .yml 中配置属性 oss.aliyun.endPoint 时，
     * Spring 就会加载 AliyunOssProperties
     * oss.aliyun.enabled=true
     * @ConditionalOnProperty(name ="oss.aliyun.enabled", matchIfMissing = true)
     * @ConditionalOnProperty(prefix = "oss.aliyun", name = "enabled")
     * @ConditionalOnProperty(name = "oss.aliyun.enabled")
     * @ConditionalOnProperty(name = "oss.aliyun.enabled", havingValue = "true")
     * @ConditionalOnProperty(name = "oss.aliyun.enabled", havingValue = "false")
     */
    @ConditionalOnProperty(value = "oss.aliyun.endPoint")
    public AliyunOssProperties aliyunOssProperties() {
        return new AliyunOssProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "oss.minio")
    /**
     * @ConditionalOnProperty(value = "oss.minio.endPoint") 表示，
     * 在配置文件 .properties 或 .yml 中配置属性 oss.aliyun.endPoint 时，
     * Spring 就会加载 AliyunOssProperties
     */
    @ConditionalOnProperty(value = "oss.minio.endPoint")
    public MinioOssProperties minioOssProperties() {
        return new MinioOssProperties();
    }

    @Bean
    /**
     * @ConditionalOnBean(value = AliyunOssProperties.class)
     * AliyunOssProperties当前被注入，将会注入 OssService 的实现 AliyunOssServiceImpl
     */
    @ConditionalOnBean(value = AliyunOssProperties.class)
    public OssService aliyunOssService(AliyunOssProperties properties) {
        return new AliyunOssServiceImpl(properties);
    }

    @Bean
    /**
     * @ConditionalOnBean(value = MinioOssProperties.class)
     * MinioOssProperties，将会注入 OssService 的实现 AliyunOssServiceImpl
     */
    @ConditionalOnBean(value = MinioOssProperties.class)
    public OssService minioOssService(MinioOssProperties properties) {
        return new MinioServiceImpl(properties);
    }

}
